import axios from 'axios'

const api = axios.create({
  baseURL: '/api',
  timeout: 30000
})

// 响应拦截器
api.interceptors.response.use(
  response => response.data,
  error => {
    console.error('API Error:', error)
    return Promise.reject(error)
  }
)

// 设备相关API
export const deviceApi = {
  getDevices: (params) => api.get('/devices', { params }),
  getDevice: (id) => api.get(`/devices/${id}`),
  createDevice: (data) => api.post('/devices', data),
  updateDevice: (id, data) => api.put(`/devices/${id}`, data),
  deleteDevice: (id) => api.delete(`/devices/${id}`),
  testConnection: (id) => api.post(`/devices/${id}/test`),
  triggerBackup: (id) => api.post(`/devices/${id}/backup`),
  debugDevice: (id) => api.post(`/devices/${id}/debug`),
  getDeviceTypes: () => api.get('/device-types')
}

// 备份相关API
export const backupApi = {
  getBackups: (params) => api.get('/backups', { params }),
  getBackup: (id) => api.get(`/backups/${id}`),
  getBackupContent: (id) => api.get(`/backups/${id}/content`),
  compareBackups: (id1, id2) => api.get(`/backups/compare/${id1}/${id2}`),
  deleteBackup: (id) => api.delete(`/backups/${id}`),
  triggerAllBackup: () => api.post('/backup/trigger-all')
}

// 统计相关API
export const statsApi = {
  getStatistics: () => api.get('/statistics'),
  getScheduleLogs: (params) => api.get('/schedule-logs', { params })
}

// 设置相关API
export const settingsApi = {
  getSettings: () => api.get('/settings'),
  updateBackupInterval: (hours) => api.post('/settings/backup-interval', null, { params: { hours } })
}

// 备份清理相关API
export const cleanupApi = {
  cleanupAll: () => api.post('/backup/cleanup'),
  cleanupDevice: (deviceId) => api.post(`/backup/cleanup/${deviceId}`),
  getBackupStats: () => api.get('/backup/stats'),
  updateMaxBackups: (maxBackups) => api.put('/backup/cleanup/max-backups', null, { params: { max_backups: maxBackups } }),
  getCleanupConfig: () => api.get('/backup/cleanup/config')
}

// 系统信息相关API
export const systemApi = {
  getSystemInfo: () => api.get('/system/info'),
  getSystemBasic: () => api.get('/system/basic'),
  getSystemCpu: () => api.get('/system/cpu'),
  getSystemMemory: () => api.get('/system/memory'),
  getSystemDisk: () => api.get('/system/disk'),
  getSystemNetwork: () => api.get('/system/network')
}

export default api
